package cn.zh.stan.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.GItem;
import cn.zh.stan.R;
import cn.zh.stan.activity.DtailGoods;
import cn.zh.stan.util.ToastUtil;
import it.sephiroth.android.library.picasso.Picasso;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by WYN on 2018/1/15 0015.
 */

public class GdAdapter extends BaseAdapter{

    Context context;
    ArrayList<GItem> arrayList;
    LayoutInflater inflater;
    String uid;
    public GdAdapter(Context context,ArrayList<GItem> arrayList,String uid){
        this.context=context;
        this.arrayList=arrayList;
        inflater=LayoutInflater.from(context);
        this.uid=uid;
       }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
      }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder holder=null;
        if (null==view){
            holder=new Holder();
            view=inflater.inflate(R.layout.gp_layout,null,false);
            holder.gimga=view.findViewById(R.id.gimga);
            holder.gadcar=view.findViewById(R.id.gadcar);
            holder.gname=view.findViewById(R.id.gname);
            holder.guint=view.findViewById(R.id.guint);
            holder.gprice=view.findViewById(R.id.gprice);
            view.setTag(holder);
        }else {
            holder= (Holder) view.getTag();
        }
        Picasso.with(context).load(arrayList.get(i).getIndexURL()).into(holder.gimga);
        holder.gname.setText(arrayList.get(i).getGoodsName());
        holder.guint.setText("单位"+arrayList.get(i).getUnit());
        holder.gprice.setText("¥"+arrayList.get(i).getPrice_Detail());
        holder.gadcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String gid=arrayList.get(i).getGoodsId();
                gaddCar(gid);
            }
        });
        return view;
    }


    public class Holder{
      ImageView gimga,gadcar;
      TextView gname,guint,gprice;
    }



    //网络请求，加入购物车
    public void gaddCar(String GoodsId){
        OkHttpUtils.post(BaseUrl.goods_addcar)
                .params("userId",uid)
                .params("goodsId",GoodsId)
                .params("num", "1")
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("----------------->添加商品到购物车==="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            String Msg=js.getString("Msg");
                            ToastUtil.showShort(context,Msg);
                            if (State){

                            }
                        } catch (JSONException e) {

                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
    }



}
